package controller;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.RejectDAO;
import dao.StockDAO;
import dao.WarehouseDAO;
import model.Warehouse;

/**
 * Servlet implementation class DropRejectController
 */
public class DropRejectController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DropRejectController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String rejectId = request.getParameter("rejectId");
		String productId = request.getParameter("productId");
		String quantity = request.getParameter("quantity");
		if(new RejectDAO().updateStatus("作废", rejectId)){
			Warehouse wr = new WarehouseDAO().getByProductId(productId);
			int newQ = wr.getEnableQuantity()+Integer.parseInt(quantity);
			new WarehouseDAO().updateEnableQuantity(newQ, productId);
			System.out.println("审核成功，已作废");
			String message = URLEncoder.encode("审核成功，已作废", "UTF-8");
			response.sendRedirect("./view/error.jsp?errorInfo="+message+"&redirect=../ChuhuoshenheController");
		}else{
			System.out.println("审核失败");
			String message = URLEncoder.encode("审核失败", "UTF-8");
			response.sendRedirect("./view/error.jsp?errorInfo="+message+"&redirect=../ChuhuoshenheController");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
